package com.hsgene.user.service.impl;

import com.hsgene.user.condition.PositionCondition;
import com.hsgene.user.domain.Position;
import com.hsgene.user.dto.orginazation.PositionDto;
import com.hsgene.user.persistence.PositionRepository;
import com.hsgene.user.persistence.UserRepository;
import com.hsgene.user.service.PositionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @projectName: hdas-geneshop-server
 * @package: com.hsgene.order.service.impl
 * @author: maodi
 * @createDate: 2018/11/5 10:14
 * @version: 1.0
 * Copyright: Copyright (c) 2018
 */
@Service
public class PositionServiceImpl implements PositionService {

    @Resource
    PositionRepository positionRepository;

    @Resource
    UserRepository userRepository;

    /**
     * @param condition 职位条件
     * @return int
     * @description 职位条件查询数量
     * @author maodi
     * @createDate 2018/11/6 16:46
     */
    @Override
    public int queryPositionCountByCondition(PositionCondition condition) {
        return positionRepository.queryPositionCountByCondition(condition);
    }

    /**
     * @param condition 职位条件
     * @return java.util.List<com.hsgene.user.dto.orginazation.PositionDto>
     * @description 职位条件查询
     * @author maodi
     * @createDate 2018/11/6 16:48
     */
    @Override
    public List<PositionDto> queryPositionByCondition(PositionCondition condition) {
        return positionRepository.queryPositionByCondition(condition);
    }

    /**
     * @param id 职位id
     * @return java.util.List<com.hsgene.user.dto.orginazation.PositionDto>
     * @description 职位根据id查询
     * @author maodi
     * @createDate 2018/11/6 16:48
     */
    @Override
    public List<PositionDto> queryPositionById(String id) {
        return positionRepository.queryPositionById(id);
    }

    /**
     * @param positionName 职位名称
     * @param companyId    公司id
     * @param departmentId 部门id
     * @return int
     * @description 职位名称，部门id，公司id查询数量
     * @author maodi
     * @createDate 2018/11/6 18:06
     */
    @Override
    public int queryPositionCountByName(String positionName, String companyId, String departmentId) {
        return positionRepository.queryPositionCountByName(positionName, companyId, departmentId);
    }

    /**
     * @param positionName 职位名称
     * @param companyId    公司id
     * @param departmentId 部门id
     * @param positionId   职位id
     * @return int
     * @description 职位名字，部门id，公司id除本身外查询数量
     * @author maodi
     * @createDate 2018/11/7 15:11
     */
    @Override
    public int queryPositionCountByNameExcludeSelf(String positionName, String companyId, String departmentId, String
            positionId) {
        return positionRepository.queryPositionCountByNameExcludeSelf(positionName, companyId, departmentId,
                positionId);
    }

    /**
     * @param companyId    公司id
     * @param departmentId 部门id
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @description 根据公司id和部门id获取职位下拉框数据源
     * @author maodi
     * @createDate 2018/11/13 18:10
     */
    @Override
    public List<Map<String, Object>> selectPosition(String companyId, String departmentId) {
        return positionRepository.selectPosition(companyId, departmentId);
    }

    /**
     * @param departmentId 部门id
     * @return int
     * @description 查询部门id在职位中使用数量
     * @author maodi
     * @createDate 2018/11/7 18:43
     */
    @Override
    public int queryCountByDepartmentId(String departmentId) {
        return positionRepository.queryCountByDepartmentId(departmentId);
    }

    /**
     * @param companyId 公司id
     * @return int
     * @description 查询公司id在职位中使用数量
     * @author maodi
     * @createDate 2018/11/7 18:43
     */
    @Override
    public int queryCountByCompanyId(String companyId) {
        return positionRepository.queryCountByCompanyId(companyId);
    }

    /**
     * @param position 职位
     * @return void
     * @description 职位新增
     * @author maodi
     * @createDate 2018/11/6 16:49
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public int insertPosition(Position position) {
        return positionRepository.insertPosition(position);
    }

    /**
     * @param position 职位
     * @return void
     * @description 职位修改
     * @author maodi
     * @createDate 2018/11/6 16:49
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public int updatePosition(Position position) {
        userRepository.updatePositionNameByPositionId(position.getPositionName(), position.getId());
        return positionRepository.updatePosition(position);
    }

    /**
     * @param position 职位
     * @return void
     * @description 职位删除
     * @author maodi
     * @createDate 2018/11/6 16:50
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public int deletePosition(Position position) {
        return positionRepository.deletePosition(position);
    }

    /**
     * @param positionIds 职位id数组
     * @return int
     * @description 职位批量删除
     * @author maodi
     * @createDate 2018/11/13 17:04
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public int deletePositionMulti(String[] positionIds) {
        return positionRepository.deletePositionMulti(positionIds, new Date());
    }

    /**
     * @param positionId 职位id
     * @return java.lang.Integer
     * @description 查询职位id在系统用户表中使用数量
     * @author maodi
     * @createDate 2018/11/29 11:35
     */
    @Override
    public Integer queryOperationAccountCountByPositionId(String positionId) {
        return positionRepository.queryOperationAccountCountByPositionId(positionId);
    }
}
